<?php
class PublicAction extends BaseAction {

    public function chkreg(){
        $account = I('account');
        $password = I('password');
        $repassword = I('repassword');
        $email = I('email');

        if ($this->apps['isreg'] == 0) {
            $this->error('系统不允许注册');
        }

        if ($email == "" || $account == "" || $password == "") {
            $this->error('登录帐号 登录密码 ,邮箱均不能为空');
            return;
        }
        if ($password != $repassword) {
            $this->error('两次输入的密码不一致！！！');
            return;
        }

        if (strlen($password) < 6) {
            $this->error('密码长度不能小于6位');
            return;
        }

        $Member = M('Member');
        $map['account']  = $account;
        $vo = $Member->where($map)->find();
        if ($vo) {
            $this->error('登陆账号已存在，请重新选择');
            return;
        }
        unset($map);
        $map['email'] = $email;
        $vo = $Member->where($map)->find();
        if ($vo) {
            $this->error('邮箱已存在，请重新选择');
            return;
        }
        $data['email'] = $email;
        $data['account'] = $account;
        $data['password'] = md5($password);
        $data['email'] = $email;
        $data['ctime'] = time();
        $vo = $Member->add($data);
        if ($vo !== false) {
            session('apps_mid',$vo['id']);
            $this->success('恭喜你，注册成功，正在跳转页面',U('Member/Index/index'));
        }
    }

	// 登录检测
    public function checkLogin() {
        if(empty($_POST['account'])) {
            $this->error('帐号错误！');
        }elseif (empty($_POST['password'])){
            $this->error('密码必须！');
        }

        $account  = $this->_post('account');
		$password  = $this->_post('password');
		$password = md5($password);
        
		$map['account'] = $account;
        $map['password'] = $password;
		$Member = D('Member');
		$member = $Member->where($map)->find();

		if ($member) {
            session('apps_mid',$member['id']);
            $this->success('登录成功！',U('Member/Index/index'));
		}else{
            $this->error('登陆账号或密码错误.请重新登陆');
		}
    }
    public function reg(){
        if ($this->apps['isreg'] == 0) {
            $this->error('系统不允许注册');
        }
    }

    // 用户登出
    public function logout() {
        cookie('apps_mid',null);
        cookie(null);
        session(null);
        $this->redirect('Member/Public/login');
    }

   
    public function resetpassword(){
        $model = M('Member');
        $check = I('check');
        $mid = substr(I('check'),64);
        $map['id'] = $mid;
        $password = I("password");
        if($password[0] != $password[1]){
            $this->error('两次输入的密码不一致！！！');
        }
        $checkstr = md5(date('Y-m-d',time())).md5(date('H',time())).$mid;
        if($checkstr != $check){
            $this->error('验证地址已经失效，，，请重新验证');
        }
        $password = md5($password[1]);
        if($model->where($map)->save(array('password'=>$password))){
            $this->success('密码修改成功！！！',U('/Public/login'));
        }else{
            $this->error('密码修改失败！！！');
        }
    }
}